Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NITWORK/SERVER-NETWORK: Fix wrong implementation of tick and receiveClientEnemyDeath #117

Merged
merged 7 commits into from
Oct 20, 2023

Conversation

romainpanno
Copy link
Collaborator

@romainpanno romainpanno commented Oct 19, 2023

PATCH

  • Please check if the PR fulfills these requirements
  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Norm of the code has been respected
  • In what state is your pull request?
  • Ready to merge / Waiting a reviwer to see my work.
  • Work In Progress (WIP) / My work is not finish but i want daily reviews. (Draft)
  • CI Review / I want to see what the CI thinks of my work.
  • Need feedback / Just to have feedback on what i produced. (May not be merged)
  • What kind of change does this PR introduce? (You can choose multiple)
  • Bug fix
  • Feature request
  • New / Updated documentation
  • Testing CI ( Make the pull request in draft mode)
  • What is the current behavior? (link an issue based on the kind of change this pr introduce)

  • What is the new behavior (if this is a feature change)?

  • Other information:

Summary by CodeRabbit

  • Refactor: Updated the internal timing system for improved precision and flexibility. This change may enhance the responsiveness and performance of the software.
  • New Feature: Added default values for thread number and tick duration parameters in server start function, simplifying the server setup process.
  • Refactor: Optimized enemy identification process in the network system, potentially improving server performance during high-action gameplay.
  • Refactor: Updated client and server start functions to align with the new timing system. This change should be transparent to users but is part of the overall performance improvement effort.

@romainpanno romainpanno added the enhancement New feature or request label Oct 19, 2023
@romainpanno romainpanno self-assigned this Oct 19, 2023
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 19, 2023

Walkthrough

The changes primarily revolve around the modification of time-related constants and parameters in the Nitwork namespace. The TICKS_PER_SECOND constant has been redefined, and a new TICK constant has been introduced. These changes affect the startClient and startServer functions in NitworkClient and NitworkServer classes respectively, and the sleep duration in ANitwork.cpp.

Changes

File Path Summary
src/Nitwork/Nitwork.h Redefined TICKS_PER_SECOND and introduced TICK constant.
src/Nitwork/NitworkClient.hpp Changed default value of tick parameter in startClient function.
src/Nitwork/NitworkServer.hpp Modified startServer function to include default values for threadNb and tick parameters.
src/Server/Systems/Network/ServerNetwork.cpp Refactored handleEnemyDeath function to use a loop instead of std::find_if.
src/main_client.cpp & src/main_server.cpp Adjusted function calls to startClient and startServer to reflect changes in their signatures.
src/Nitwork/ANitwork.cpp Adjusted sleep duration calculation to use new time-related constants.

🐇
"In the land of code, where the shadows lie,
Changes come, under the cloudless sky.
Constants shift, functions renew,
A rabbit hops, reviewing what's new.
Tick-tock, the clock, in the code it weaves,
A tale of time, in the land of leaves." 🍃


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • Mention @coderabbitai in any review comment for bot assistance.
  • Note: Review comments are made on code diffs or files, not on the PR overview.

Pause Incremental Reviews

  • Insert @coderabbitai: ignore in the PR description to halt the bot's ongoing reviews. Remove the line to resume.
  • Additionally, enter @coderabbitai pause to halt the bot's ongoing reviews as any issue or review comment. To
    resume reviews, enter @coderabbitai resume as any issue or review comment.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Commits Files that changed from the base of the PR and between 760990e and 7e00f94.
Files selected for processing (2)
  • src/Nitwork/Nitwork.h (1 hunks)
  • src/Server/Systems/Network/ServerNetwork.cpp (1 hunks)
Additional comments (Suppressed): 2
src/Nitwork/Nitwork.h (1)
  • 15-15: The change in the value of TICKS_PER_SECOND from 60 to 1/256 (or 256 if the above issue is addressed) is a significant change. Ensure that this change is intentional and that it does not negatively impact any code that relies on the TICKS_PER_SECOND macro.
src/Server/Systems/Network/ServerNetwork.cpp (1)
  • 49-71: The refactoring of the handleEnemyDeath function to use a loop instead of std::find_if is a good performance optimization. However, the new code assumes that the enemy IDs are the same as the array indices, which might not always be the case. If the IDs and indices are not guaranteed to be the same, this could lead to incorrect behavior. Please verify this assumption.
- for (auto &id : ids) {
-     if (arrEnemies[id].getConstId().id == msgEnemyDeath.enemyId.id) {
+ for (size_t i = 0; i < arrEnemies.size(); ++i) {
+     if (arrEnemies[i].getConstId().id == msgEnemyDeath.enemyId.id) {

src/Nitwork/Nitwork.h Outdated Show resolved Hide resolved
src/Nitwork/Nitwork.h Outdated Show resolved Hide resolved
src/Server/Systems/Network/ServerNetwork.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between 7e00f94 and 46c54ce.
Files selected for processing (1)
  • src/Server/Systems/Network/ServerNetwork.cpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/Server/Systems/Network/ServerNetwork.cpp

@romainpanno romainpanno changed the title NITWORK/SERVER-NETWORK: Fix wrong implementation of tick and NITWORK/SERVER-NETWORK: Fix wrong implementation of tick and receiveClientEnemyDeath Oct 19, 2023
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between 46c54ce and 254bda6.
Files selected for processing (5)
  • src/Nitwork/Nitwork.h (1 hunks)
  • src/Nitwork/NitworkClient.hpp (1 hunks)
  • src/Nitwork/NitworkServer.hpp (1 hunks)
  • src/main_client.cpp (1 hunks)
  • src/main_server.cpp (1 hunks)
Files skipped from review due to trivial changes (2)
  • src/Nitwork/Nitwork.h
  • src/Nitwork/NitworkServer.hpp
Additional comments (Suppressed): 3
src/Nitwork/NitworkClient.hpp (1)
  • 29-29: The tick parameter's default value has been updated from TICKS_PER_SECOND to TICK. Ensure that this change is reflected in all calls to this function throughout the codebase and that the new TICK value is appropriate for the intended functionality.
src/main_client.cpp (1)
  • 49-50: The startClient function now takes TICK as the last argument instead of TICKS_PER_SECOND. Ensure that this change is intentional and that the TICK value is correctly calculated and used throughout the codebase.
src/main_server.cpp (1)
  • 56-56: The startServer function now takes two additional arguments: DEFAULT_THREAD_NB and TICK. Ensure that these new parameters are correctly initialized and used throughout the codebase. Also, verify that the changes do not break any existing functionality or cause any side effects.
-    if (!Nitwork::NitworkServer::getInstance().startServer(std::stoi(av[1]), std::stoi(av[2]))) {
+    if (!Nitwork::NitworkServer::getInstance().startServer(std::stoi(av[1]), std::stoi(av[2]), DEFAULT_THREAD_NB, TICK)) {

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between 254bda6 and 055323d.
Files selected for processing (3)
  • src/Nitwork/NitworkClient.hpp (1 hunks)
  • src/Nitwork/NitworkServer.hpp (1 hunks)
  • src/main_server.cpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/Nitwork/NitworkClient.hpp
Additional comments (Suppressed): 2
src/main_server.cpp (1)
  • 56-57: The startServer function now accepts two additional arguments: DEFAULT_THREAD_NB and TICK. Ensure that these new parameters are correctly defined and initialized before this function call. Also, verify that the function signature of startServer has been updated to accept these additional parameters without breaking existing functionality.
src/Nitwork/NitworkServer.hpp (1)
  • 24-24: The startServer function signature has been updated to use TICK instead of TICKS_PER_SECOND as the default value for the tick parameter. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, verify that the new TICK value does not negatively impact the server's performance or functionality.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Commits Files that changed from the base of the PR and between 055323d and 67082d7.
Files selected for processing (1)
  • src/Nitwork/Nitwork.h (1 hunks)
Additional comments (Suppressed): 1
src/Nitwork/Nitwork.h (1)
  • 16-16: The TICKS_PER_SECOND constant has been updated from 60 to 128. This change will affect the rate at which the game server updates, potentially improving the responsiveness of the game. Ensure that this change does not introduce performance issues, such as increased CPU usage or network traffic.

#define ONE_SECOND 1000
#define TICKS_PER_SECOND 128
#define TICK (ONE_SECOND / (TICKS_PER_SECOND * ONE_SECOND))
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TICK constant has been introduced, calculated based on TICKS_PER_SECOND. However, the current calculation seems incorrect as it divides ONE_SECOND by the square of TICKS_PER_SECOND. This would result in a much smaller value than expected. The correct calculation should be ONE_SECOND / TICKS_PER_SECOND.

-    #define TICK (ONE_SECOND / (TICKS_PER_SECOND * ONE_SECOND))
+    #define TICK (ONE_SECOND / TICKS_PER_SECOND)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai its because one second is in milliseconds, and ticks per second is 128 so i need to convert it in miliseconds by doing * 1000 is it correct ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romainpanno You're correct that ONE_SECOND is defined as 1000, representing one second in milliseconds. However, the current calculation for TICK is still incorrect.

The purpose of TICK is to represent the duration of one tick in milliseconds. If TICKS_PER_SECOND is 128, then there are 128 ticks in one second. Therefore, the duration of one tick should be ONE_SECOND / TICKS_PER_SECOND, which gives the number of milliseconds per tick.

The current calculation ONE_SECOND / (TICKS_PER_SECOND * ONE_SECOND) is equivalent to 1 / TICKS_PER_SECOND, which would give a very small value in seconds, not milliseconds.

So, the correct calculation should indeed be ONE_SECOND / TICKS_PER_SECOND. This will give the duration of one tick in milliseconds, which seems to be what you're aiming for. Here's the corrected line:

-    #define TICK (ONE_SECOND / (TICKS_PER_SECOND * ONE_SECOND))
+    #define TICK (ONE_SECOND / TICKS_PER_SECOND)

This will ensure that TICK correctly represents the duration of one tick in milliseconds.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romainpanno Great! If you have any more questions or need further clarification, feel free to ask. Happy coding!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Commits Files that changed from the base of the PR and between 67082d7 and f42dc95.
Files selected for processing (6)
  • src/Nitwork/ANitwork.cpp (1 hunks)
  • src/Nitwork/Nitwork.h (1 hunks)
  • src/Nitwork/NitworkClient.hpp (1 hunks)
  • src/Nitwork/NitworkServer.hpp (1 hunks)
  • src/main_client.cpp (1 hunks)
  • src/main_server.cpp (1 hunks)
Additional comments (Suppressed): 6
src/main_client.cpp (1)
  • 50-50: The startClient function call has been updated to use TICKS instead of TICKS_PER_SECOND. Ensure that the TICKS macro is correctly defined and that this change does not affect the expected behavior of the startClient function.
src/main_server.cpp (1)
  • 56-57: The startServer function now takes two additional parameters: DEFAULT_THREAD_NB and TICKS. Ensure that these parameters are correctly defined and initialized before this function call. Also, verify that the function signature of startServer has been updated to accept these additional parameters and that it handles them correctly.
src/Nitwork/NitworkServer.hpp (1)
  • 24-24: The default value of the tick parameter in the startServer function has been changed from TICKS_PER_SECOND to TICKS. Ensure that this change is reflected in all calls to this function throughout the codebase. Also, verify that the new TICKS value is correctly calculated and suitable for the game's tick mechanism.
src/Nitwork/NitworkClient.hpp (1)
  • 24-26: The function signature for startClient has been changed. The tick parameter's default value has been updated from TICKS_PER_SECOND to TICKS. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, verify that the new TICKS value is correctly calculated and used in the context of this function.
src/Nitwork/ANitwork.cpp (1)
  • 60-66: The new hunk introduces a macro TICKS_PER_MILLISECOND to calculate the sleep duration for the clock thread. Ensure that this macro is correctly defined and that it provides the expected value for the sleep duration. Also, consider using std::lock_guard or std::unique_lock for managing the mutex to ensure that it gets unlocked even if an exception is thrown.
- _tickMutex.lock();
- _tickConvVar.notify_all();
- _tickMutex.unlock();
+ std::lock_guard<std::mutex> lock(_tickMutex);
+ _tickConvVar.notify_all();
src/Nitwork/Nitwork.h (1)
  • 14-17: The new definitions of TICKS, TICKS_PER_SECOND, and TICKS_PER_MILLISECOND seem to change the previous behavior. Ensure that these changes are propagated throughout the codebase and that they do not introduce any unexpected behavior, especially in time-sensitive operations. Also, the TICKS_PER_SECOND and TICKS_PER_MILLISECOND macros now require an argument t, which was not the case before. Make sure that all calls to these macros have been updated accordingly.

Comment on lines +15 to +17
#define TICKS 128
#define TICKS_PER_SECOND(t) (t / TICKS)
#define TICKS_PER_MILLISECOND(t) (TICKS_PER_SECOND(t) / 1000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TICKS_PER_SECOND and TICKS_PER_MILLISECOND macros are now dependent on the TICKS macro. This introduces a coupling between these macros. If TICKS is changed in the future, it will affect the other two macros. Consider if this coupling is desired or if it would be better to keep these values independent.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Commits Files that changed from the base of the PR and between f42dc95 and a0b5348.
Files selected for processing (1)
  • src/Nitwork/NitworkClient.hpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/Nitwork/NitworkClient.hpp

@guillaumeAbel guillaumeAbel merged commit 5c6a3ba into dev Oct 20, 2023
@Saverio976 Saverio976 deleted the fix/Nitwork-tiks-and-enemydeath branch March 4, 2024 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants